package com.intelli4.pos.ui;

import javax.swing.JFrame;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.intelli4.pos.events.AfterSessionCloseEvent;
import com.intelli4.pos.events.AfterSessionOpenEvent;
import com.intelli4.pos.events.SessionEvent;
import com.intelli4.pos.listeners.SessionListener;

public class PointOfSaleApplication implements SessionListener {
	private WelcomeFrame welcomeFrame;
	private CashierFrame cashierFrame;

	public PointOfSaleApplication() {
	}

	public void setWelcomeFrame(WelcomeFrame welcomeFrame) {
		this.welcomeFrame = welcomeFrame;
	}

	public void setCashierFrame(CashierFrame cashierFrame) {
		this.cashierFrame = cashierFrame;
	}

	@Override
	public boolean onEvent(SessionEvent event) {
		if (event instanceof AfterSessionOpenEvent) {
			cashierFrame.setSize(1024, 768);
			cashierFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			cashierFrame.setVisible(true);
		}

		if (event instanceof AfterSessionCloseEvent) {
			cashierFrame.setVisible(false);
			welcomeFrame.setVisible(true);
		}

		return true;
	}

	public void run() {
		welcomeFrame.setSize(1024, 768);
		welcomeFrame.setVisible(true);
	}

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"pos-context.xml");
		PointOfSaleApplication app = (PointOfSaleApplication) ctx
				.getBean("posApp");
		app.run();
	}
}
